#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QTimer"
#include <QDebug>

#include "image_proc.h"
#include "get_yuv.h"
#include "yuv2rgb.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTimer *sensorPollTimer = new QTimer(this);
    connect(sensorPollTimer, SIGNAL(timeout()), this, SLOT(sensorPoll()));
    sensorPollTimer->start(900);

    pollTask = new PollThread;
    pollTask->start();

    camError = 0;

#ifdef linux
    camError = init_camera("/dev/video0", 320, 240);
    /* if error */
#else
    QCameraViewfinderSettings set;
    set.setResolution(320, 240);

    camInfoList = QCameraInfo::availableCameras();
    if (camInfoList.size() == 0) {
        camError = 1;
    } else {
        cam = new QCamera(camInfoList.at(0));
        cam->setViewfinderSettings(set);
        cam->start();

        cap = new QCameraImageCapture(cam);
        cap->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
        connect(cap, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));
    }
#endif /* linux */

    if (! camError) {
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(camCapture()));
        timer->start(40);
    }
}

MainWindow::~MainWindow()
{
    delete ui;

#ifdef linux
    if (! camError)
        exit_camera();
#endif
}

void MainWindow::camCapture()
{
    if (camError)
        return;
#ifdef linux
    unsigned char *buf;
    QImage rawImage(320, 240, QImage::Format_RGB32);

    buf = (unsigned char *)malloc(320*240*2);
    get_frame(buf, 320*240*2);
    convert_yuv_to_rgb888_buffer(buf, rawImage.bits(), 320, 240);
    /* if error */
    free(buf);

    displayImage(0, rawImage);
#else
    cap->capture("cap.jpg");
#endif /* linux */
}

void MainWindow::sensorPoll()
{
    int index = 1;
    QString style;

    style.append("<font style = 'font-size:24px; color:#24A5FA;'><b>温度\uf2c8：</b>");
    vector<double> temperature = pollTask->getTemperature();
    foreach (double tmp, temperature) {
        style.append("<div font style = 'font-size:20px; color:#08907C; font-family:Agency FB;'>");
        style.append("Sensor " + QString::number(index++) + "： " + QString::number(tmp) + "℃");
        style.append("</div>");
    }
    style.append("</font><p>");

    style.append("<font style = 'font-size:24px; color:#24A5FA;'><b>亮度\uf185：</b>");
    vector<int> luma = pollTask->getLight();
    foreach (double tmp, luma) {
        style.append("<div font style = 'font-size:20px; color:#08907C; font-family:Agency FB;'>");
        style.append("Sensor " + QString::number(index++) + "： " + QString::number(tmp));
        style.append("</div>");
    }
    style.append("</font>");

    ui->textLabel->setText(style);
}

void MainWindow::displayImage(int, QImage image)
{
    QImage grayImage = image.convertToFormat(QImage::Format_Grayscale8);
    QImage edgeImage(grayImage.size(), QImage::Format_Grayscale8);
    QImage maskImage = image;

    sobel_edge_detection(grayImage.bits(), edgeImage.bits(), grayImage.width(), grayImage.height(), 60);
    //canny_edge_detection(grayImage.bits(), edgeImage.bits(), grayImage.width(), grayImage.height(), 60);
    bin_img_reverse(edgeImage.bits(), edgeImage.bits(),edgeImage.width(), edgeImage.height());
    maskImage = edgeImage.convertToFormat(image.format());
    curImage = maskImage;
    and_operation(image.bits(), maskImage.bits(), curImage.bits(), image.width()*2, image.height());

    ui->snapLabel->setPixmap(QPixmap::fromImage(curImage).scaled(ui->snapLabel->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
